<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\LawyerCase;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LawyerCaseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LawyerCase(), function (Grid $grid) {
            $grid->addTableClass(['table-text-center']);
//            $grid->fixColumns(1, 0);
            $grid->withBorder();
            $grid->column('id')->width(80)->sortable();
            $grid->column('name')->width(160);
            $grid->column('author')->width(100);
            $grid->column('cover')->image('', 60,60);
            $grid->column('description')->width(160);
//            $grid->column('essay');
            $grid->column('crime_time');
            $grid->column('views');
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->like('name','案件名称');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new LawyerCase(), function (Show $show) use($id) {
            $show->field('id');
            $show->field('name');
            $show->field('author');
            $show->field('cover')->image();
            $show->field('description');
            $show->field('essay')->link("/case/detail/{$id}");
//            $show->html(function ($detail){
//                return $detail['essay'];
//            });
            $show->field('crime_time');
            $show->field('views');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new LawyerCase(), function (Form $form) {
            $form->display('id');
            $form->text('name')->rules('required');
            $form->text('author')->disable()->default(Admin::user()->getAttribute('name'));
            $form->image('cover')->rules('required')->uniqueName()->removable(false)->autoUpload()->thumbnail('small', $width = 30, $height = 30);
            $form->text('description')->rules('required');
            $form->editor('essay')->rules('required');
            $form->datetime('crime_time')->rules('required');
            $form->text('views')->rules('required');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
